/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bankmanagement;

import java.util.*;
import java.util.Date;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
 *
 * @author NDA
 */
public class Account {
    
    
    
     String temp;    
     int accountNumber;
     String password;
     String accountName;
     boolean accountType;
     boolean gender;
     Date dateOfBirth;
     String email;
     double balance;
    
    public Date convertStringToDate(String str) throws ParseException{
        String format = "dd-MM-yyyy";
        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
    try {
        return mySimpleDateFormat.parse(str);
    } catch (ParseException e) {
        return null;
    }
    }
    
    public String convertDateToString(Date date){
        String format = "dd-MM-yyyy";
        SimpleDateFormat mySimpleDateFormat = new SimpleDateFormat(format);
        return mySimpleDateFormat.format(date);
    }
    
    
    public Account(int accountNumber, String password, String accountName, boolean accountType, 
            boolean gender, Date dateOfBirth, String email, double balance){
        this.accountNumber = accountNumber;
        this.password = password;
        this.accountName = accountName;
        this.accountType = accountType;
        this.gender = gender;
        this.dateOfBirth = dateOfBirth;   
        this.email = email;
        this.balance = balance;
    }
    
    public Account(){
        
    }
    
    public void writeData(PrintWriter out) throws IOException{
        temp = convertDateToString(dateOfBirth);
        out.println(accountNumber + "|" + password + "|" + accountName + "|" + 
                accountType + "|" + gender + "|" + temp + "|" + email + "|" + balance);
    }
    
    public void readData(BufferedReader in) throws IOException, ParseException {
        
        String s = in.readLine();
        StringTokenizer t = new StringTokenizer(s, "|");
        accountNumber = Integer.parseInt(t.nextToken());
        password = t.nextToken();
        accountName = t.nextToken();
        accountType = Boolean.parseBoolean(t.nextToken());
        gender = Boolean.parseBoolean(t.nextToken());
        temp = t.nextToken();
        dateOfBirth = convertStringToDate(temp);
        email= t.nextToken();
        balance = Double.parseDouble(t.nextToken());
        
    }
    public void printData(){
        temp = convertDateToString(dateOfBirth);
        String sGender;
        String sAccountType;
        String sAccountNumber = Integer.toString(accountNumber);
        if(gender) sGender = "Male"; else sGender = "Female";
        if(accountType) sAccountType = "Admin"; else sAccountType = "Customer";
        String sBalance = Double.toString(balance);
        System.out.printf("|%15s|%12s|%18s|%14s|%8s|%14s|%25s|%11s|\n",sAccountNumber ,password ,accountName
                , sAccountType , sGender, temp, email, sBalance);
    }
}